# -*- coding: utf-8; mode: snippet -*-
# name: <div ng-model="..." filter-user-input /> 
# key: adirective
# contributor: Chen Bin <chenbin DOT sh AT gmail>
# --
// <div ng-model="..." number-only ></div>
// could be a general directive eating illegal characters
.directive('${1:numberOnly}', function() {
  function link(scope, element, attrs, ctrl) {
    // ngModel from {require:'ngModel'}
    // @see https://docs.angularjs.org/guide/directive
    // analyze user input, so user need press key or move mouse at first
    ctrl.$parsers.push(function (origVal) {
      if (!origVal) {
          return '';
        }
      var newVal = origVal.replace(/[^0-9]/g, '');
      if (newVal !== origVal) {
      ctrl.$setViewValue(newVal);
      ctrl.$render();
      }
      return newVal;
    });
  }
  return {
    restrict: 'A', // A - match attribute, E - match element name, C, match attribute name
    require: 'ngModel',
    link: link
  };
})